<?php
namespace Apps\AppApi\Method\BeautyCanon;

use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Models\CosBeautyCanon;
use Apps\Common\Models\CosPointCanon;
use Apps\Common\Libs\PicUrl;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\CosBeautyCanonAssist;

/*
 * 美粒宝典列表:通过城市编码筛选
 */
class GetList extends BaseMethod{
	private $bcanon = [];
	
	public function run() 
	{
		$city_code = $this->getDataItem('city_code',0);
		//上传手机唯一标识
		$phone_id = $this->getDataItem('phone_id',"");
	
		$cityId = $this->getCityId($city_code);
	
		if($cityId <= 0) {
			//return $this->errorLog(ResultStatus::CITY_NOT_OPEN, '城市未开通');
			$cityId=2;
		}
						
		$this -> bcanon = CosPointCanon::query()
			->columns([
				'bc.id',
				'bc.title',
				//'bc.content',
				'bc.img_url',
				'bc.settime',
				'bc.fit',
			    'summary',
				'bc.assist'
				//'bc.assist',
				//'bc.setman',
				//'Apps\Common\Models\CosPointCanon.sort as sort'
			])
			-> leftJoin('Apps\Common\Models\CosBeautyCanon',
					'Apps\Common\Models\CosPointCanon.canon_id=bc.id','bc')
			-> where('Apps\Common\Models\CosPointCanon.open_city_id = '.$cityId.' and bc.state = 2')
			-> orderBy('Apps\Common\Models\CosPointCanon.sort desc')
			-> execute()->toArray();
		$this->bcanon = $this->bcanon ? $this->bcanon : [];
	
		for($i = 0; $i < count($this->bcanon); $i++) {
		    
		    //判断是否已经点过赞
		    $bc_assist = CosBeautyCanonAssist::count([
		        "conditions"=>"mobile_sign = :sign: AND beauty_canon_id = :bc_id: ",
		        "bind" => [
		            'sign' => $phone_id,
		            'bc_id' => $this -> bcanon[$i]['id'],
		        ],
		    ]);
		    if ($bc_assist>0){
		        $this->bcanon[$i]['is_assist'] = true;
		    }
		    
		    if($this -> bcanon[$i]['summary'] == null){
		        $this -> bcanon[$i]['summary'] = '';
		    }
// 			$this->bcanon[$i]['img_url'] = PicUrl::BeautyCanonCover($this->bcanon[$i]['img_url'], $this->getDi());
			$this->bcanon[$i]['img_url'] = "http://img.meelier.com/".$this->bcanon[$i]['img_url'];
			if($this -> bcanon[$i]['fit'] == null){
				$this -> bcanon[$i]['fit'] = 0;
			}
			if($this->bcanon[$i]['assist'] == null){
				$this -> bcanon[$i]['assist'] = 0;
			}
		}
		$this->setResult( $this->bcanon);
	}
	
	
	
	
	
}